package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.YxUserFeedbackInfo;
import co.yixiang.modules.shop.repository.YxUserFeedbackRepository;
import co.yixiang.modules.shop.service.YxUserFeedbackService;
import co.yixiang.modules.shop.service.dto.YxUserFeedbackDto;
import co.yixiang.modules.shop.service.dto.YxUserFeedbackQueryCriteria;
import co.yixiang.modules.shop.service.mapper.YxUserFeedbackMapper;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import co.yixiang.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

/**
 * @ClassName : YxUserFeedbackServiceImpl
 * @Description :
 * @Author : DragonUncle
 * @Date :  17:12 2020/5/27
 */
@Component
public class YxUserFeedbackServiceImpl implements YxUserFeedbackService {


    private final YxUserFeedbackRepository yxUserFeedbackRepository;

    private final YxUserFeedbackMapper yxUserFeedbackMapper;

    public YxUserFeedbackServiceImpl(YxUserFeedbackMapper yxUserFeedbackMapper, YxUserFeedbackRepository yxUserFeedbackRepository) {
        this.yxUserFeedbackMapper = yxUserFeedbackMapper;
        this.yxUserFeedbackRepository = yxUserFeedbackRepository;
    }

    @Override
    public Map queryAll(YxUserFeedbackQueryCriteria feedbackDto, Pageable pageable) {
        Page<YxUserFeedbackInfo> all = yxUserFeedbackRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,feedbackDto,criteriaBuilder),pageable);
        return PageUtil.toPage(all.map(yxUserFeedbackMapper::toDto));
    }

    @Override
    public void update(YxUserFeedbackInfo resources) {
        Optional<YxUserFeedbackInfo> byId = yxUserFeedbackRepository.findById(resources.getId());
        ValidationUtil.isNull( byId,"YxUserFeedbackInfo","id",resources.getId());
        YxUserFeedbackInfo yxUserFeedbackInfo = byId.get();
        yxUserFeedbackInfo.copy(resources);
        yxUserFeedbackRepository.save(yxUserFeedbackInfo);
    }

    @Override
    public void delete(YxUserFeedbackInfo resources) {
        Optional<YxUserFeedbackInfo> byId = yxUserFeedbackRepository.findById(resources.getId());
        ValidationUtil.isNull( byId,"YxUserFeedbackInfo","id",resources.getId());
        YxUserFeedbackInfo yxUserFeedbackInfo = byId.get();
        yxUserFeedbackInfo.copy(resources);
        yxUserFeedbackRepository.delete(yxUserFeedbackInfo);
    }


}
